package com.sg.java.util;


import com.sg.java.entity.Gx;
import com.sg.java.entity.VoltageQualified;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

@Log4j
public class EntityUtils {

//    private static final Logger log = LoggerFactory.getLogger(EntityUtils.class);


    public static Gx StringToYhGx(String s) {
        String[] split = s.split("!@@");
        String up = split[1] + "U";
        String down = split[1] + "D";
        String sn = split[3].replaceAll(",", "");
        String userName = sn.replaceAll("'", "");
        String userId = split[0];
        String orgNo = split[2];
        //电网类型
        if (StringUtils.isBlank(up) || StringUtils.isBlank(down)) {
            return null;
        }
        String gridType = split[4];
        if (StringUtils.isEmpty(gridType) || gridType.equals("02") || gridType.equals("03") || gridType == null || gridType.equals("null") ) {
            gridType = "2";
        } else if (gridType.equals("01")) {
            gridType = "1";
        }

        Double pt = 1D;
        if (split[8] != null && !split[8].equals("null")) {
            pt = Double.parseDouble(split[8]);
        }
        return Gx.builder().up(up).down(down).userName(userName).userId(userId).orgNo(orgNo).dydj(split[1]).gridType(gridType).pid(split[5]).wiringMode(split[7]).mpVoltLevel(split[6]).consVoltLevel(split[1]).pt(pt).build();
    }


    public static Gx StringToYhGx2(String s) {
        String[] split = s.split("!@@");
        String up = split[1] + "U";
        String down = split[1] + "D";
        String sn = split[3].replaceAll(",", "");
        String userName = sn.replaceAll("'", "");
        String userId = split[0];
        String orgNo = split[2];
        //电网类型

        if (StringUtils.isBlank(up) || StringUtils.isBlank(down)) {
            return null;
        }

        String gridType = split[4];
        if (StringUtils.isEmpty(gridType) || gridType.equals("02") || gridType.equals("03") || gridType == null  || gridType.equals("null") ) {
            gridType = "2";
        } else if (gridType.equals("01")) {
            gridType = "1";
        }


        return Gx.builder().up(up).down(down).userName(userName).userId(userId).orgNo(orgNo).dydj(split[1]).gridType(gridType).pid(split[5]).build();
    }

    public static Gx StringToPbGx(String s) {
        String[] split = s.split("!@@");
        String up = split[1] + "U";
        String down = split[1] + "D";
        String pbId = split[0];
        String sn = split[2].replaceAll(",", "");
        String pbName = sn.replaceAll("'", "");
        String orgNo = split[3];
        String gridType = split[5];

        if (StringUtils.isEmpty(gridType) || gridType.equals("1") || gridType == null || gridType.equals("null")) {
            gridType = "2";
        } else if (gridType.equals("0")) {
            gridType = "1";
        }

        return Gx.builder().up(up).down(down).pbId(pbId).tranName(pbName).orgNo(orgNo).dydj(split[1]).gridType(gridType).build();
    }


    public static VoltageQualified initVoltageQualified() {
        VoltageQualified voltageQualified = VoltageQualified.builder()
                .upTime(0D)
                .downTime(0D)
                .monitorTime(0D)
                .maxUOffset(0)
                .minUOffset(0)
                .offset(1)
                .totalU(0D)
                .notNullPoint(0D)
                .effectiveDataTime(0D)
                .yzyxTime(0)
                .realVoltagePoint(0D)
//                .pbArray("")
                .gaoSum(0)
                .diSum(0)
                .yzGaoSum(0)
                .yzDiSum(0)
                .highTime(0D)
                .lowTime(0D)
                .severeHighTime(0D)
                .severelyLowTime(0D)
                .build();

        //用户长期高电压点数集合
        ArrayList<Integer> yhLongTimeUperArray = new ArrayList<Integer>();

        //用户长期低电压点数集合
        ArrayList<Integer> yhLongTimeDownArray = new ArrayList<Integer>();

        //用户严重高电压点数集合
        ArrayList<Integer> yhSeverityUperArray = new ArrayList<Integer>();

        //用户严重低电压点数集合
        ArrayList<Integer> yhSeverityDownArray = new ArrayList<Integer>();

        //越高数组
        StringJoiner highVoltage = new StringJoiner(",");

        //越低数组
        StringJoiner lowVoltage = new StringJoiner(",");

        //严重越高数组
        StringJoiner severeHighVoltage = new StringJoiner(",");

        //严重越低数组
        StringJoiner severelyLowVoltage = new StringJoiner(",");

        //监测时间数组
        StringJoiner monitoringTimeArray = new StringJoiner(",");

        //电压全部原始值
        StringJoiner voltArray = new StringJoiner(",");


        StringBuilder upBuilder = new StringBuilder();
        StringBuilder downBuilder = new StringBuilder();
        voltageQualified.setUpBuilder(upBuilder);
        voltageQualified.setDownBuilder(downBuilder);

        //监测时间集合
        ArrayList<Integer> monitoringTimeList = new ArrayList<>();
        addList(monitoringTimeList);

        voltageQualified.setVoltArray(voltArray);
        voltageQualified.setYhLongTimeUperArray(yhLongTimeUperArray);
        voltageQualified.setYhLongTimeDownArray(yhLongTimeDownArray);
        voltageQualified.setYhSeverityUperArray(yhSeverityUperArray);
        voltageQualified.setYhSeverityDownArray(yhSeverityDownArray);
        voltageQualified.setHighVoltage(highVoltage);
        voltageQualified.setLowVoltage(lowVoltage);
        voltageQualified.setSevereHighVoltage(severeHighVoltage);
        voltageQualified.setSeverelyLowVoltage(severelyLowVoltage);
        voltageQualified.setMonitoringTimeArray(monitoringTimeArray);
        return voltageQualified;
    }

    public static void addList(ArrayList<Integer> list) {
        for (int i = 0; i < 24; i++) {
            list.add(0);
        }
    }

}
